var __extends = this.__extends || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
};
var TSUI;
(function (TSUI) {
    (function (UI) {
        var DropDownBox = (function (_super) {
            __extends(DropDownBox, _super);
            function DropDownBox() {
                _super.call(this);
                this.OnSelectedIndexChange = new TSUI.Events.SelectedIndexChangeEvent();
                this._JustClosedItems = false;
                this._HideItemsTimeout = -1;
                this._IgnoreNextBlur = false;

                this._rootElement.addClass("DropDownBox");

                this._InputContentPanel = new UI.Panel();
                this.Children.Add(this._InputContentPanel);

                this._UnderlyingTextBox = new UI.Label();
                this._UnderlyingTextBox.Focusable(true);
                this._UnderlyingTextBox.OnFocus.Attach(new TSUI.Events.FocusEventHandler(this._UnderlyingControl_OnFocus, this));
                this._UnderlyingTextBox.OnBlur.Attach(new TSUI.Events.BlurEventHandler(this._UnderlyingControl_OnBlur, this));
                this._InputContentPanel.Children.Add(this._UnderlyingTextBox);

                this._DropDownToggle = new UI.ToggleIndicatorLabel();
                this._DropDownToggle.SetIndicatorToOff();
                this._DropDownToggle.Focusable(false);
                this._DropDownToggle.OnClick.Attach(new TSUI.Events.ClickEventHandler(this._DropDownToggle_OnClick, this));
                this._InputContentPanel.Children.Add(this._DropDownToggle);

                this.Focusable(true);

                this._SelectionWindow = new UI.SelectionWindow();
                this._SelectionWindow.ContentPanel.OnSelectionMade.Attach(new TSUI.Events.SelectionMadeEventHandler(this.SelectionWindow_OnSelectionMade, this));
                this._SelectionWindow.ContentPanel.OnSelectedIndexChange.Attach(new TSUI.Events.SelectedIndexChangeEventHandler(this.SelectionWindow_OnSelectedIndexChanged, this));
                this._SelectionWindow.OnClose.Attach(new TSUI.Events.CloseEventHandler(this.SelectionWindow_Close, this));
                this.Items = this._SelectionWindow.ContentPanel.Items;
                this._SelectionWindow.ConstructDOM($("body"));
            }
            DropDownBox.prototype._HandleFocusableSet = function (focusable) {
                this._rootElement.removeAttr("tabindex");
            };

            DropDownBox.prototype.SelectionWindow_Close = function (eventArgs) {
                this._JustClosedItems = true;
                this.Focus();
            };
            DropDownBox.prototype.SelectionWindow_OnSelectionMade = function (eventArgs) {
                this.HideItems();
            };
            DropDownBox.prototype.SelectionWindow_OnSelectedIndexChanged = function (eventArgs) {
                this._UnderlyingTextBox.Text(this.Items.ElementAt(this.SelectedIndex()).Text());
                this.OnSelectedIndexChange.Invoke(new TSUI.Events.SelectedIndexChangeEventArgs(this));
            };

            DropDownBox.prototype._OnFocus = function (event) {
                _super.prototype._OnFocus.call(this, event);
                UI.preventTabKey = true;
                if (this._HideItemsTimeout !== -1) {
                    clearTimeout(this._HideItemsTimeout);
                    this._HideItemsTimeout = -1;
                }
                if (this.ActuallyEnabled()) {
                    if (!this._SelectionWindow.Visible() && !this._JustClosedItems) {
                        this._IgnoreNextBlur = true;
                        this.ShowItems();
                    }
                }

                this._JustClosedItems = false;
            };
            DropDownBox.prototype._OnBlur = function (event) {
                if (!this._IgnoreNextBlur) {
                    _super.prototype._OnBlur.call(this, event);
                    UI.preventTabKey = false;
                    if (this._HideItemsTimeout !== -1) {
                        clearTimeout(this._HideItemsTimeout);
                        this._HideItemsTimeout = -1;
                    }
                    if (this._SelectionWindow.Visible()) {
                        var _this = this;
                        this._HideItemsTimeout = setTimeout(function () {
                            _this.HideItems();
                        }, 50);
                    }
                }
                this._IgnoreNextBlur = false;
            };

            DropDownBox.prototype._DropDownToggle_OnClick = function (event) {
                this._UnderlyingTextBox.Focus();
                TSUI.StopEvent(event.jqEvent);
            };
            DropDownBox.prototype._UnderlyingControl_OnFocus = function (eventArgs) {
                this._OnFocus(eventArgs.jqEvent);
            };
            DropDownBox.prototype._UnderlyingControl_OnBlur = function (eventArgs) {
                this._OnBlur(eventArgs.jqEvent);
            };

            DropDownBox.prototype.SelectedIndex = function (value) {
                if (typeof value === "undefined") { value = null; }
                return this._SelectionWindow.ContentPanel.SelectedIndex(value);
            };

            DropDownBox.prototype.Focus = function () {
                this._UnderlyingTextBox.Focus();
            };
            DropDownBox.prototype.Blur = function () {
                this._UnderlyingTextBox.Blur();
            };

            DropDownBox.prototype.ShowItems = function () {
                this._SelectionWindow.PositionCenterWindow();
                this._SelectionWindow.Show();
                this._DropDownToggle.SetIndicatorToOn();
                this.AddClass("Open");
            };
            DropDownBox.prototype.HideItems = function () {
                this._SelectionWindow.Hide();
                this._DropDownToggle.SetIndicatorToOff();
                this.RemoveClass("Open");
            };

            DropDownBox.prototype.Enabled = function (value) {
                var result = _super.prototype.Enabled.call(this, value);
                if (!result && this._SelectionWindow.Visible()) {
                    this.HideItems();
                }
                return result;
            };
            return DropDownBox;
        })(UI.Control);
        UI.DropDownBox = DropDownBox;
    })(TSUI.UI || (TSUI.UI = {}));
    var UI = TSUI.UI;
})(TSUI || (TSUI = {}));
